var waitTime = 60;
function sendTime(o) {
	if (waitTime == 0) {
		o.removeAttribute("disabled");
		o.value = "获取验证码";
		waitTime = 60;
	} else {
		o.setAttribute("disabled", true);
		o.value = "重新发送(" + waitTime + ")";
		waitTime--;
		setTimeout(function() {
			sendTime(o)
		}, 1000);
	}
}

$(function() {
	$('.imgcode').click(function() {
		var url = ctx + "captcha?type=math&s=" + Math.random();
		$(".imgcode").attr("src", url);
	});
	validTypeVisible();
	$("#validType").change(function(){
		validTypeVisible();
	});
	validateRule();
	$('#btnReset').click(function(){
		location = '/';
	});
	$('#sendFpValidCode').click(function() {
		var $this = this;
		var val = $("#validType").val();
		var loginname = $.common.trim($("input[name='loginname']").val());
		var validatecode = $.common.trim($("input[name='validateCode']").val());
		if(loginname == '') {
			$.modal.alertWarning("请输入登录名");
			return false;
		}
		if(validatecode == '') {
			$.modal.alertWarning('请输入验证码');
			return false;
		}
		sendTime($this);
		$.ajax({
			type : "post",
			url : ctx + "admin/user/getFpValidCode",
			data : {
				"loginname" : loginname,
				"op" : val,
				"validatecode" : validatecode
			},
			success : function(r) {
				if (r.code == 0) {
					$.modal.msg("验证码已发送！");
				} else {
					$.modal.closeLoading();
					$.modal.msg(r.msg);
					waitTime = 0;
				}
			}
		});
	});
});

function validTypeVisible() {
	var val = $("#validType").val();
	var txt = (val == 'mobile' ? '手机' : '邮箱');	
	$('#fpValidCode').attr('placeholder', txt+'验证码').attr('data-msg-required', '请填写'+txt+'验证码.');;
	$('#sendFpValidCode').val('获取'+txt+'验证码');
}

$.validator.setDefaults({
	submitHandler : function() {
		if($.validate.form()) {
			resetPwd();
		}
	}
});

function resetPwd() {
	$.modal.loading($("#btnSubmit").data("loading"));
	var loginname = $.common.trim($("input[name='loginname']").val());
	var password = $.common.trim($("input[name='password']").val());
	$.ajax({
		type : "post",
		url : ctx + "admin/user/modifyPwd",
		data : {
			"loginname" : loginname,
			"password" : algorithmRequestParam(password)
		},
		success : function(r) {
			if (r.code == 0) {
				layer.alert("<font color='red'>恭喜你，您的密码已修改</font>", {
					icon : 1,
					title : "系统提示"
				}, function(index) {
					// 关闭弹窗
					layer.close(index);
					location.href = ctx + 'index';
				});
			} else {
				$.modal.closeLoading();
				$.modal.msg(r.msg);
			}
		}
	});
}

function validateRule() {
	$("#forgetForm").validate({
		rules : {
			loginname : {
				required : true,
				minlength : 2,
				remote : {
					type : "post",
					url : ctx + "admin/user/checkHasAccount",
					data : {
						accountName : function() {
							return $("#loginname").val();
						}
					}
				}
			},
			validateCode : {
				required : true,
				remote : {
					type : "post",
					url : ctx + "admin/user/checkCode",
					data : {
						curCode : function() {
							return $("#validateCode").val();
						}
					}
				}
			},
			fpValidCode : {
				remote : {
					type : "post",
					url : ctx + "admin/user/checkFpValidCode",
					data : {
						curCode : function() {
							return $("#fpValidCode").val();
						}
					}
				}
			},
			password : {
				required : true,
				minlength : 6
			},
			confirmPassword : {
				required : true,
				equalTo : "[name='password']"
			}
		},
		messages : {
			loginname : {
				required : "请输入您的登录名",
				minlength : "登录名不能小于2个字符",
				remote : "登录名不存在"
			},
			validateCode : {
				required : "验证码不能为空",
				remote : "请输入正确的验证码"
			},
			fpValidCode : {
				remote : "请输入正确的验证码"
			},
			password : {
				required : "请输入您的密码",
				minlength : "密码不能小于6个字符",
			},
			confirmPassword : {
				required : "请再次输入您的密码",
				equalTo : "两次密码输入不一致"
			}
		}
	})
}
